<!-- 车辆管理 -->
<template>
  <div class="dashboard-container driver customer-list-box">
    <el-card class="card">
      <!-- 搜索表单 -->
      <el-form
        ref="form"
        :model="formData"
        label-width="90px"
      >
        <el-row
          type="flex"
          justify="space-between"
        >
          <el-col :span="7">
            <el-form-item
              class="item"
              label="司机账号:"
              prop="account"
            >
              <el-input
                v-model="formData.account"
                placeholder="请输入司机账号"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="7">
            <el-form-item
              class="item"
              label="司机姓名:"
              prop="name"
            >
              <el-input
                v-model="formData.name"
                placeholder="请输入司机姓名"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="7">
            <el-form-item
              class="item"
              label="司机手机号:"
              prop="mobile"
            >
              <el-input
                v-model="formData.mobile"
                placeholder="请输入司机手机号"
              ></el-input>
            </el-form-item>
          </el-col>

        </el-row>

        <el-row
          type="flex"
          justify="space-between"
        >
          <el-col :span="7">
            <el-form-item
              class="item"
              label="所属机构:"
              prop="agentId"
            >
              <Treeselect
                v-model="formData.agentId"
                :options="agencyOptions"
                :normalizer="normalizer"
                :searchable="true"
                style="width: 100%"
                placeholder="请选择所属机构"
                no-options-text="暂无数据"
                no-results-text="暂无数据"
                @select="selectAgency"
                @open="open"
                @close="close"
              >
              </Treeselect>
              <img
                ref="arrow"
                src="../../assets/pullDown.png"
                style="position:absolute;width:18px;height:18px;right:9px;top:12px;cursor:pointer;pointer-events:none;transition-duration:0.3s"
              />
            </el-form-item>
          </el-col>
          <el-col
            :span="7"
            style="margin-left:20px"
          >
            <el-button
              type="danger"
              @click="findDriverList()"
            >搜索</el-button>
            <el-button @click="formreset()">重置</el-button>
          </el-col>
          <el-col :span="7">
          </el-col>
        </el-row>
      </el-form>
    </el-card>
    <el-card class="card">
      <!-- 司机列表 -->
      <el-table
        :data="list"
        stripe
        style="width: 100%"
      >
        <el-table-column
          label="序号"
          width="100"
        >
          <template v-slot="{row}">
            {{ list.indexOf(row)+1+((queryParams.page-1)*queryParams.pageSize) }}
          </template>
        </el-table-column>
        <el-table-column
          prop="account"
          label="司机账号"
          width="200"
        >
        </el-table-column>
        <el-table-column
          prop="name"
          label="司机姓名"
          width="200"
        >
        </el-table-column>
        <el-table-column
          prop="mobile"
          label="司机电话"
          width="200"
        >
        </el-table-column>
        <el-table-column
          prop="truck.licensePlate"
          label="关联车辆"
          width="200"
        >
        </el-table-column>
        <el-table-column
          prop="agency.name"
          label="所属机构"
          width="200"
        >
        </el-table-column>
        <el-table-column
          label="工作状态"
          width="200"
        >
          <template v-slot="{row}">
            <span
              v-if="row.workStatus===1"
              class="workStatusYes"
            >上班</span>
            <span v-else><i class="workStatusNo"></i> 休息</span>
          </template>
        </el-table-column>
        <el-table-column
          fixed="right"
          label="操作"
          width="200"
        >
          <template
            v-slot="{row}"
          >
            <el-button
              type="text"
              size="small"
              @click="handleClick(row.userId)"
            >查看</el-button>
            <el-button
              type="text"
              size="small"
              @click="OptionsOk(row)"
            >配置车辆</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页器 -->
      <el-row
        type="flex"
        justify="center"
        style="margin:20px 0 40px"
      >

        <el-pagination
          layout="total, sizes, prev, pager, next, jumper"
          :total="counts"
          :page-sizes="[10,20,30,50]"
          :current-page="queryParams.page"
          :page-size="queryParams.pageSize"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </el-row>
    </el-card>
    <!-- 分配车辆 -->
    <el-dialog
      title="配置车辆"
      :visible.sync="carConfigvisible"
      width="700px"
    >
      <!-- 司机信息 -->
      <div class="driverMessage">
        <el-row
          type="flex"
          justify="space-between"
          style="padding-left:50px;margin-bottom:20px"
        >
          <el-col :span="12">
            <span>司机编号：{{ driverMessage.userId }}</span>
          </el-col>
          <el-col :span="12">
            <span>司机名称：{{ driverMessage.name }}</span>
          </el-col>
        </el-row>
        <el-row
          type="flex"
          justify="space-between"
          style="padding-left:50px"
        >
          <el-col :span="12">
            <span>司机电话：{{ driverMessage.mobile }}</span>
          </el-col>
          <el-col :span="12">
            <span>所属机构：{{ driverMessage.agency.name }}</span>
          </el-col>
        </el-row>
      </div>
      <!-- 选择框 -->
      <el-row style="margin:30px">
        <span style="font-weight:700;">配置车辆：</span>
        <el-select
          v-model="carselect"
          placeholder="请选择"
          style="width:50%"
          @change="carDetailSelect(carselect)"
        >
          <el-option
            v-for="item in carList"
            :key="item.id"
            :label="item.licensePlate"
            :value="item.id"
            width="60%"
          >
          </el-option>
        </el-select>
      </el-row>
      <!-- 表单 -->
      <el-table
        :data="carMessage"
        border
        style="width: 100%"
      >
        <el-table-column
          fixed
          prop="licensePlate"
          label="车牌号"
        >
        </el-table-column>
        <el-table-column
          fixed
          prop="workStatus"
          label="车辆状态"
        >
          <template v-slot="{row}">
            <span>{{ row.workStatus===1? '启用' : '停用' }}</span>
          </template>
        </el-table-column>
        <el-table-column
          fixed="right"
          label="操作"
          header-align="center"
        >
          <template slot-scope="scope">
            <el-button
              type="text"
              size="small"
              @click="car(scope.row)"
            >查看 </el-button>
            <span style="margin:0 5px">|</span>
            <el-button
              type="text"
              size="small"
              style="color:red;"
              @click="deleteCar()"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-row
        type="flex"
        justify="center"
        style="margin-top:30px"
      >
        <el-button
          type="danger"
          @click="driverArrangeCar()"
        >确认</el-button>
        <el-button @click="driverArrangeCarCancel()">取消</el-button>
      </el-row>
    </el-dialog>

  </div>
</template>
<script>
import { driverList, getTruckListInDriver, truckTypeDetail, driverArrangeCar } from '@/api/transit'
import { agencyList } from '@/api/institutions'
import Treeselect from '@riophae/vue-treeselect'
import '@riophae/vue-treeselect/dist/vue-treeselect.css'
export default {
  name: 'Courses',
  components: { Treeselect },
  data() {
    return {
      normalizer(node) {
        return {
          id: node.id,
          label: node.name,
          children: node.children
        }
      },
      formData: {
        account: '', // 司机账号
        name: '', // 司机姓名
        mobile: '', // 司机账号
        agentId: null
      },
      counts: 5,
      queryParams: {
        page: 1,
        pageSize: 10
      },
      list: [],
      agencyOptions: [],
      carConfigvisible: false,
      driverMessage: {
        account: '', // 司机账号
        name: '', // 司机姓名
        mobile: '', // 司机账号
        agency: {
          name: ''
        } // 司机账号
      },
      carselect: '',
      carList: [],
      carMessage: []
    }
  },
  created() {
    this.getdriverList()
    this.getagencyList()
  },
  methods: {
    // 渲染司机列表
    async getdriverList() {
      const { data } = await driverList(this.queryParams)
      console.log(data)
      this.list = data.items
      this.counts = +data.counts
      console.log(this.list)
      console.log(this.counts)
    },
    // 获取树状结构
    async getagencyList () {
      const { data } = await agencyList()
      this.agencyOptions = JSON.parse(data)
    },
    open() {
      this.$refs.arrow.style.transform = 'rotate(-180deg)'
    },
    close() {
      this.$refs.arrow.style.transform = 'rotate(0deg)'
    },
    selectAgency(val) {
      this.formData.agentId = val.id
    },
    // 切换每页数量
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`)
      this.queryParams.pageSize = val
      this.queryParams.page = 1
      this.getdriverList()
    },
    // 换页
    handleCurrentChange(val) {
      this.queryParams.page = val
      this.getdriverList()
    },
    // 重置
    formreset() {
      this.formData = {
        account: '', // 司机账号
        name: '', // 司机姓名
        mobile: '', // 司机账号
        agentId: null // 司机账号
      }
      this.$refs.form.resetFields()
      this.queryParams.page = 1
      this.getdriverList()
    },
    // 搜索
    async findDriverList() {
      const { data } = await driverList({ ...this.formData, ...this.queryParams })
      this.list = data.items
      this.counts = +data.counts
      // console.log(data)
    },
    // 查看
    async handleClick(id) {
      // console.log(id)
      localStorage.setItem('driverId', id)
      // this.$store.commit('setting/setDriverId', id)
      this.$router.push(`driver-detail?id=${id}&formUrlName=司机管理`, id)
    },
    // 配置车辆弹框
    OptionsOk(row) {
      this.$confirm(`
      <p>配置车辆需满足以下条件：</p>
      <p>1司机信息已完善&nbsp&nbsp&nbsp&nbsp2司机已设置排班</p>
      <p>3司机绑定车辆未在启用状态</p>
      `, '车辆配置', {
        confirmButtonText: '确定',
        dangerouslyUseHTMLString: true,
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        console.log(row)
        this.driverMessage = row
        console.log('driverMessage', this.driverMessage)
        try {
          const { data } = await getTruckListInDriver()
          if (this.driverMessage.truck) {
            console.log(1)
            const { data: res } = await truckTypeDetail(this.driverMessage.truck.id)
            this.carMessage.splice(0, 1, res)
            this.carselect = this.driverMessage.truck.licensePlate
          }
          this.carList = data
          this.carConfigvisible = true
        } catch (err) {
          console.log(err)
        }
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消'
        })
      })
    },
    // 更改车辆
    async carDetailSelect(id) {
      const { data } = await truckTypeDetail(id)
      this.carMessage.splice(0, 1, data)
    },
    async driverArrangeCar() {
      const res = await driverArrangeCar({ driverIds: [+this.driverMessage.account], truckId: +this.carselect })
      if (res.code === 1) {
        this.$message.error(res.msg)
      } else {
        this.$message.success('操作成功')
        this.driverArrangeCarCancel()
      }
    },
    driverArrangeCarCancel() {
      this.carConfigvisible = false
      this.carMessage = []
      this.carselect = ''
    },
    deleteCar() {
      this.carselect = ''
      this.carMessage = []
    },
    car(item) {
      const id = item.id
      console.log(id, '111')
      localStorage.setItem('truckLicenseId', id)
      // this.$store.commit('setting/truckLicenseId', id)
      this.$router.push(`/transit/vehicle-detail/${id}`, id)
    }
  }
}
</script>
<style rel="stylesheet/scss" lang="scss" scoped>
.driverMessage {
  margin: 0 auto;
  padding: 20px;
  background: rgb(251,250,250);
}
.alert {
  margin: 10px 0px;
}
.pagination {
  margin-top: 40px;
}
.card {
  margin: 20px;
}

// 下拉框
/deep/ .el-select {
  width: 100%;
}

/deep/ .el-card{
  overflow: initial;
}

</style>
<style lang="scss">
.item .el-form-item__label {
      font-weight: normal;
    }
</style>
